package com.powergisol.gis.feature;

import com.powergisol.gis.geom.GeoType;
import com.vividsolutions.jts.geom.Geometry;
import org.geotools.data.collection.ListFeatureCollection;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import java.util.*;

public class SimpleFeatureGenerator {

    private String crs;

    private String charSet;

    private String name;


    public SimpleFeatureGenerator() {

        this.crs = "EPSG:4326";
        this.name = "type";
        this.charSet = "UTF-8";
    }

    public SimpleFeatureGenerator(String name, String crs, String charSet) {
        this.charSet = charSet;
        this.name = name;
        this.crs = crs;
    }


    public SimpleFeatureCollection createCollection(List<Geometry> geometrys,String[] names, List<Object[]> values, String geom) {


        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();

        //set global state
        builder.setName(name);
        builder.setNamespaceURI("http://www.powergisol.com/");
        builder.setSRS(crs);

        geom = geom==null?"the_geom":geom;

        if(!values.isEmpty()&&!geometrys.isEmpty()){
            Object[] value = values.get(0);
            builder.add(geom, geometrys.get(0).getClass());

            if(names.length == value.length){

                for(int i=0;i<names.length;i++){
                    builder.add(names[i], value[i].getClass());
                }

            }else {
                throw new IllegalArgumentException("names 和 vlue 的数量必须一致");
            }


        }

        List<SimpleFeature> features = new ArrayList<>();

        SimpleFeatureType featureType = builder.buildFeatureType();
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);

        if(geometrys.size()==values.size()){

            for(int i=0;i<geometrys.size();i++){
                featureBuilder.add(geometrys.get(i));

                featureBuilder.addAll(values.get(i));

                SimpleFeature feature = featureBuilder.buildFeature(null);
                features.add(feature);
            }



        }else{

            throw new IllegalArgumentException("几何数量和属性的数量必须一致");
        }

        SimpleFeatureCollection collection = new ListFeatureCollection(featureType, features);

        return collection;

    }

    /**
     *
     * @param values
     * @return
     */
    public SimpleFeatureCollection createCollection(List<? extends GeoType> values){

        return createCollection(values,"the_geom");
    }


    public SimpleFeatureCollection createCollection(List<? extends GeoType> values, String geom) {


        if(!values.isEmpty()){

            String[] names =  null;
            List<Geometry> geometrys = new ArrayList<>();
            List<Object[]> objects = new ArrayList<>();

            for(int i=0;i<values.size();i++){

                GeoType geoType = values.get(i);
                Properties properties = geoType.getProperties();
                Geometry   geometry = geoType.toGeometry();
                if(i==0){
                    Set<String> strings = properties.stringPropertyNames();
                    names = strings.toArray(new String[strings.size()]);
                }

                Object[] obs = properties.values().toArray(new Object[properties.values().size()]);
                geometrys.add(geometry);
                objects.add(obs);
            }

            return createCollection(geometrys,names,objects,geom);

        }

        return null;

    }


}
